package com.woniumall.web;

import com.woniumall.pojo.Cart;
import com.woniumall.pojo.User;
import com.woniumall.service.CartService;
import com.woniumall.service.impl.CartServiceImpl;


import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.List;

/**
 * @auther linke
 * @create 2021-10-31 17:40
 */
@WebServlet("/cart/*")
public class CartServlet extends BaseServlet {
    private CartService cartService = new CartServiceImpl();
    public void joinCart(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //获取请求参数
        String gid = request.getParameter("gid");
        String nums = request.getParameter("nums");
        //获取uid
        User user = (User)request.getSession().getAttribute("user");
        int uid = user.getId();

        cartService.joinCart(uid,Integer.parseInt(gid),Integer.parseInt(nums));
    }

    public void findAllByUid(HttpServletRequest request, HttpServletResponse response) throws Exception {
       //获取Uid
        User user = (User)request.getSession().getAttribute("user");
        request.getSession().removeAttribute("cartlist");//清空已有购物侧信息

        if (user == null){//如果用户未登录，跳转至登录页面
                response.sendRedirect(request.getContextPath()+"/jspPage/login.jsp");
        }else {//已登录，进入购物车清单页面
            int userId = user.getId();

            List<Cart> cartlist = cartService.findAllByUid(userId);
            for (Cart cart : cartlist) {
                BigDecimal nums = new BigDecimal(cart.getNums());
                BigDecimal subtotal = nums.multiply(cart.getPrice());
                cart.setSubtotal(subtotal);
                System.out.println(nums);
                System.out.println(subtotal);
            }
            System.out.println(cartlist);

            request.getSession().setAttribute("cartlist",cartlist);//设置购物车信息
            request.getRequestDispatcher("/jspPage/mycart.jsp").forward(request,response);
        }

    }
    //查询用户订单总数
    public void countOrder(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //获取uid
        User user = (User)request.getSession().getAttribute("user");
        //如果用户未登录则返回0
        if(user == null){//
            response.getWriter().write(""+0);
        }else {
            int userId = user.getId();
            int orderNums = cartService.countOrderByUserId(userId);
            response.getWriter().write(""+orderNums);
        }
    }

    //根据购物车列表cartId删除数据
    public void remove(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //获取cartId
        String cartId = request.getParameter("cartId");
        //调用删除方法
       cartService.remove(Integer.parseInt(cartId));
        //刷新页面
        findAllByUid(request,response);
    }

    //页面数量改变，同步到数据库中的购物车数量
    public void synchronization(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //获取cartId
        String cartId = request.getParameter("cartId");
        //获取cartId
        String nums = request.getParameter("nums");

        //调用修改方法，同步数量
        cartService.updateNums(Integer.parseInt(cartId),Integer.parseInt(nums));
        //响应回200
        response.getWriter().write("200");
    }

}
